package gui;

import java.util.GregorianCalendar;

import msjcalendar.Calendar;

import org.eclipse.jface.viewers.ISelectionChangedListener;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.jface.viewers.TableViewerColumn;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ControlEvent;
import org.eclipse.swt.events.ControlListener;
import org.eclipse.swt.graphics.FontMetrics;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Table;

import enums.DayEnum;

/**
 * @author markus
 *
 */
public class WeekViewComposite extends Composite {

	/**
	 * tables for show events.
	 */
	private TableViewer[] tables;
	/**
	 * 
	 */
	private GregorianCalendar date;
	/**
	 * 
	 */
	private Calendar calendar;
	/**
	 * 
	 */
	private ISelectionChangedListener selectionListener;
	
	/**
	 * @return the calendar
	 */
	public Calendar getCalendar() {
		return calendar;
	}


	/**
	 * @param calendar the calendar to set
	 */
	public void setCalendar(final Calendar calendar) {
		this.calendar = calendar;
	}


	/**
	 * @param parent parent	control
	 * @param style SWT-style
	 * @param calendar calendar
	 * @param listener selectionchangedlistener to add to all tables
	 */
	public WeekViewComposite(final Composite parent, final int style, final Calendar calendar, 
			final ISelectionChangedListener listener) {
		super(parent, style);
		GridLayout layout = new GridLayout(6, true);
		this.setLayout(layout);
		this.selectionListener = listener;
		this.tables = new TableViewer[7];
		this.generateTables(); 
		this.calendar = calendar;
	}
	
	
	/**
	 * @param inputdate date
	 * @return first day of the week of the date
	 */
	public GregorianCalendar getFirstDayOfWeek(final GregorianCalendar inputdate) {
		inputdate.setFirstDayOfWeek(GregorianCalendar.MONDAY);
		if (inputdate.get(GregorianCalendar.DAY_OF_WEEK) == 1) {
			inputdate.add(GregorianCalendar.DAY_OF_WEEK, -2);
		}
		inputdate.add(GregorianCalendar.DAY_OF_MONTH, -inputdate.get(GregorianCalendar.DAY_OF_WEEK) + 2);
		return inputdate;
	}
	
	/**
	 * @param date date
	 */
	public void setDate(final GregorianCalendar date) {
		this.date = getFirstDayOfWeek(date);
		for (int i  = 0; i < 7; ++i) {
			GregorianCalendar tmpdate = (GregorianCalendar) this.date.clone();
			tmpdate.add(GregorianCalendar.DAY_OF_WEEK, i);
			String s  = DayEnum.values()[i].toString().substring(0, 2).concat(" ")
					.concat(String.valueOf(tmpdate.get(GregorianCalendar.DAY_OF_MONTH))).concat(".")
					.concat(String.valueOf(tmpdate.get(GregorianCalendar.MONTH) + 1)).concat(".");
			tables[i].getTable().getColumn(0).setText(s);
			((EventContentProvider) tables[i].getContentProvider()).setDate((GregorianCalendar) tmpdate.clone());
		}
	}
	
	/**
	 * 
	 */
	public void refresh() {
		for (int i = 0; i < 7; ++i) {
			tables[i].setInput(this.calendar);
			this.tables[i].refresh();
		}
	}
	
	/**
	 * 
	 */
	public void generateTables() {
		for (int i = 0; i < 7; ++i) {
			tables[i] = new TableViewer(this, SWT.BORDER);
			int vspan = 1;
			if (i < 5) {
				vspan = 2;
			}
			GridData data = new GridData(SWT.FILL, SWT.FILL, true, true, 
					1, vspan);
			tables[i].getTable().setLayoutData(data);
			tables[i].setContentProvider(new EventContentProvider());
			tables[i].setLabelProvider(new EventLabelProvider());
			final Table table = tables[i].getTable();
			TableViewerColumn column = new TableViewerColumn(tables[i], SWT.None);
			column.getColumn().setText("test");
			column.setLabelProvider(new EventLabelProvider());
			table.addControlListener(new ControlListener() {
				@Override
				public void controlResized(final ControlEvent e) {
					table.getColumns()[0].setWidth(table.getSize().x - 5);
				}
				@Override
				public void controlMoved(final ControlEvent e) {
					table.getColumns()[0].setWidth(table.getSize().x - 5);
					
				}
			});
			table.addListener(SWT.MeasureItem, new Listener() {
			   public void handleEvent(final Event event) {
				  FontMetrics fm = new GC(Display.getCurrent()).getFontMetrics();
				  int height = fm.getHeight();
			      event.height = 2 * height;
			   }
			});
			table.setHeaderVisible(true);
			table.setLinesVisible(false);
			tables[i].addSelectionChangedListener(this.selectionListener);
		}
	}
}

